﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Data.Entity.Design.PluralizationServices;

namespace Happy.ExtentionMethods
{
    /// <summary>
    /// 扩展了<see cref="string"/>。
    /// </summary>
    public static class StringExtensions
    {
        private static readonly PluralizationService _PluralizationService
            = PluralizationService.CreateService(CultureInfo.CreateSpecificCulture("EN"));

        /// <summary>
        /// 转换为复数形式。
        /// </summary>
        public static string ToPluralize(this string world)
        {
            world.MustNotNull(world);

            return _PluralizationService.Pluralize(world);
        }

        /// <summary>
        /// 转换为单数形式。
        /// </summary>
        public static string ToSingularize(this string world)
        {
            world.MustNotNull(world);

            return _PluralizationService.Singularize(world);
        }

        /// <summary>
        /// 是否是复数形式。
        /// </summary>
        public static bool IsPlural(this string world)
        {
            world.MustNotNull(world);

            return _PluralizationService.IsPlural(world);
        }

        /// <summary>
        /// 是否是单数形式。
        /// </summary>
        public static bool IsSingular(this string world)
        {
            world.MustNotNull(world);

            return _PluralizationService.IsSingular(world);
        }

        /// <summary>
        /// 返回字符串的省略形式。
        /// </summary>
        public static string Ellipsis(this string that, int len, string ellipsis = "...")
        {
            if (string.IsNullOrEmpty(that))
            {
                return that;
            }
            if (that.Length <= len)
            {
                return that;
            }

            return that.Substring(0, len - ellipsis.Length) + ellipsis;
        }

        /// <summary>
        /// 转换为字节。
        /// </summary>
        public static byte[] ToBytes(this string that, string encodingName = "UTF-8")
        {
            if (string.IsNullOrEmpty(that))
            {
                return new byte[] { };
            }

            return Encoding.GetEncoding(encodingName).GetBytes(that);
        }
    }
}
